package ${package.Controller};

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.bigdata.cloud.framework.common.api.HttpCodeEnum;
import net.bigdata.cloud.framework.common.api.Resp;
import net.bigdata.cloud.framework.common.controller.BaseController;
import net.bigdata.cloud.framework.common.dto.BasePageDto;
import net.bigdata.cloud.framework.common.dto.IdBaseDto;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
* @author ： ${author}
* @date ：Created in ${date}
* @description：${table.comment!} Controller接口
* @modified By：
* @version: 1.0
*/
@Slf4j
@Api(tags = "${table.comment}")
@RestController
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
public class ${table.controllerName} extends BaseController {

    @Resource
    private ${table.serviceName} ${table.serviceName?uncap_first};


    @ApiOperation(value = "${table.comment}新增")
    @PostMapping(value = "/add.do")
    public  Resp<Boolean> addByPo(@Valid @RequestBody ${entity} dto, BindingResult result) {
        try {
            if (result.hasErrors()) {
                return Resp.failed(HttpCodeEnum.VALIDATE_ERROR,result.getAllErrors().get(0).getDefaultMessage());
            }
            return ${table.serviceName?uncap_first}.addByPo(dto);
        }catch (Exception e){
            log.error("服务调用异常：", e);
            return Resp.failed(HttpCodeEnum.INVOKE_ERROR);
        }
    }

    @ApiOperation(value = "${table.comment}修改")
    @PostMapping(value = "/update.do")
    public  Resp<Boolean> updateByPo(@Valid @RequestBody ${entity} dto, BindingResult result) {
        try {
            if (result.hasErrors()) {
                return Resp.failed(HttpCodeEnum.VALIDATE_ERROR,result.getAllErrors().get(0).getDefaultMessage());
            }
            return ${table.serviceName?uncap_first}.updateByPo(dto);
        }catch (Exception e){
            log.error("服务调用异常：", e);
            return Resp.failed(HttpCodeEnum.INVOKE_ERROR);
        }
    }

    @ApiOperation(value = "${table.comment}详情")
    @PostMapping(value = "/detail.do")
    public  Resp<${entity}> detail(@Valid @RequestBody IdBaseDto dto, BindingResult result) {
        try {
            if (result.hasErrors()) {
                return Resp.failed(HttpCodeEnum.VALIDATE_ERROR,result.getAllErrors().get(0).getDefaultMessage());
            }
            return ${table.serviceName?uncap_first}.detail(dto);
        }catch (Exception e){
            log.error("服务调用异常：", e);
            return Resp.failed(HttpCodeEnum.INVOKE_ERROR);
        }
    }

    @ApiOperation(value = "${table.comment}删除")
    @PostMapping(value = "/remove.do")
    public  Resp<Boolean> removeByPo(@Valid @RequestBody IdBaseDto dto, BindingResult result) {
        try {
            if (result.hasErrors()) {
                return Resp.failed(HttpCodeEnum.VALIDATE_ERROR,result.getAllErrors().get(0).getDefaultMessage());
            }
            return ${table.serviceName?uncap_first}.removeByPo(dto);
        }catch (Exception e){
            log.error("服务调用异常：", e);
            return Resp.failed(HttpCodeEnum.INVOKE_ERROR);
        }
    }


    @ApiOperation(value = "${table.comment}分页列表")
    @PostMapping(value = "/page.do")
    public  Resp<IPage<${entity}>> getPage(@Valid @RequestBody BasePageDto dto, BindingResult result) {
        try {
            if (result.hasErrors()) {
                return Resp.failed(HttpCodeEnum.VALIDATE_ERROR,result.getAllErrors().get(0).getDefaultMessage());
            }
            return ${table.serviceName?uncap_first}.getPage(dto);
        }catch (Exception e){
            log.error("服务调用异常：", e);
            return Resp.failed(HttpCodeEnum.INVOKE_ERROR);
        }
    }



}
